package kr.ac.kaist.savehearts;

import kr.ac.kaist.savehearts.auth.UserInfo;
import android.content.Context;
import android.content.SharedPreferences;
import android.telephony.TelephonyManager;

/**
 * Configure class contains all variables which is necessary for configuring the application.
 * The class loads/saves configurations by using SharedPreference with loadConfiguration()/saveConfiguration().
 * Configure class is a singleton class which can be obtained like below:
 * 		Configure configuration = Configure.getInstance([Activity or Context]);
 * android.content.Context(or Activity) is required to initialize Configure class to retrieve
 * a phone number (TelephonyManager) and load/save configurations (SharedPreferences).
 * @author Seungwook Han (hsw1120@gmail.com)
 */

public class Configure {
	private static Configure _instance;
	public static Configure getInstance() {
		if ( _instance == null ) _instance = new Configure();
		return _instance;
	}
	
	/**
	 * Tag for debugging
	 */
	private static final String TAG = Configure.class.getSimpleName();
	
	public Configure() {
		this.loadConfiguration();
	}
	
	private Context mContext;
	public Context getContext() { return this.mContext; }
	public void setContext(Context context) { this.mContext = context; }
	
	/**
	 * Return phone number of the device.
	 * @return 
	 * 	Phone number of the device (e.g., +82-1234-5678) or
	 * 	null if the device does not have a phone number or not available for some reason.
	 */
	private String getPhoneNumber() {
		if ( this.mContext == null ) return null;
		TelephonyManager tMgr = (TelephonyManager)mContext.getSystemService(Context.TELEPHONY_SERVICE);
		return tMgr.getLine1Number();
	}
	
	public boolean loadConfiguration() {
		if ( this.mContext == null ) return false;
		
		SharedPreferences myPrefs = mContext.getSharedPreferences(SaveHeartsConstants.PREFERENCE, 
				mContext.MODE_PRIVATE);
	
		int interval = myPrefs.getInt(SaveHeartsConstants.PROP_GPS_INTERVAL,
				SaveHeartsConstants.DEFAULT_GPS_INTERVAL);
		setGPSInterval(interval);
		
		boolean gpsOn = myPrefs.getBoolean(SaveHeartsConstants.PROP_GPS_ON, true);
		isGpsOn(gpsOn);
		
		String lastName = myPrefs.getString(SaveHeartsConstants.PROP_LASTNAME, null);
		String firstName = myPrefs.getString(SaveHeartsConstants.PROP_FIRSTNAME, null);
		String fullName = myPrefs.getString(SaveHeartsConstants.PROP_FULLNAMT, null);
		String ID = myPrefs.getString(SaveHeartsConstants.PROP_ID, null);
		String Email = myPrefs.getString(SaveHeartsConstants.PROP_EMAIL, null);
		String pictureUrl = myPrefs.getString(SaveHeartsConstants.PROP_PICTURE_URL, null);
		
		int statusInterval = myPrefs.getInt(SaveHeartsConstants.PROP_STATUS_UPDATE_INTERVAL, 
				SaveHeartsConstants.DEFAULT_STATUS_UPDATE_INTERVAL_MIN);
		setStatusUpdateInterval(statusInterval);
		
		UserInfo userInfo = new UserInfo(ID, lastName, firstName, fullName, Email, pictureUrl);
		setUserInfo(userInfo);
		
		userInfo.setPhoneNumber(getPhoneNumber());
		
		return true;
	}
	
	/**
	 * saveConfiguration() saves current settings using SharedPreference for future usage.
	 * Stored settings are loaded when the application begins next time by calling loadConfiguration().
	 * @return
	 * 	true: successfully saved current settings
	 * 	false: failed to save current settings
	 */
	public boolean saveConfiguration() {
		if ( mContext == null ) return false;
		
		SharedPreferences myPrefs = mContext.getSharedPreferences(SaveHeartsConstants.PREFERENCE, 
				mContext.MODE_PRIVATE);

		SharedPreferences.Editor editor = myPrefs.edit();
		editor.putInt(SaveHeartsConstants.PROP_GPS_INTERVAL, this.getGPSInterval());
		editor.putInt(SaveHeartsConstants.PROP_STATUS_UPDATE_INTERVAL, this.getStatusUpdateInterval());
		editor.putBoolean(SaveHeartsConstants.PROP_GPS_ON, true);
		editor.putString(SaveHeartsConstants.PROP_ID, this.getUserInfo().getId());
		editor.putString(SaveHeartsConstants.PROP_LASTNAME, this.getUserInfo().getLastName());
		editor.putString(SaveHeartsConstants.PROP_FIRSTNAME, this.getUserInfo().getFirstName());
		editor.putString(SaveHeartsConstants.PROP_FULLNAMT, this.getUserInfo().getFullName());
		editor.putString(SaveHeartsConstants.PROP_EMAIL, this.getUserInfo().getEmail());
		editor.putString(SaveHeartsConstants.PROP_PICTURE_URL, this.getUserInfo().getPictureUrl());
		
		return true;
	}
	
	private boolean bGpsOn = false;
	public boolean isGpsOn() { return this.bGpsOn; }
	public void isGpsOn(boolean gps) { this.bGpsOn = gps; }
	
	private int gpsInterval = SaveHeartsConstants.DEFAULT_GPS_INTERVAL_MIN;
	public int getGPSInterval() { return this.gpsInterval; }
	public void setGPSInterval(int interval) { 
		if ( interval > SaveHeartsConstants.GPS_MAX_INTERVAL ) interval = SaveHeartsConstants.GPS_MAX_INTERVAL;
		if ( interval < SaveHeartsConstants.GPS_MIN_INTERVAL ) interval = SaveHeartsConstants.GPS_MIN_INTERVAL;
		this.gpsInterval = interval; 
	}
	
	private int statusUpdateInterval = SaveHeartsConstants.DEFAULT_STATUS_UPDATE_INTERVAL_MIN;
	public int getStatusUpdateInterval() { return this.statusUpdateInterval; }
	public void setStatusUpdateInterval(int interval) { this.statusUpdateInterval = interval; }
	
	/**
	 * Current user information (profile, location, and neighboring users)
	 */
	private UserInfo userInfo;
	public UserInfo getUserInfo() { return this.userInfo; }
	public void setUserInfo(UserInfo userInfo) { this.userInfo = userInfo; }
	
	public boolean isConfigureValid() {
		return (userInfo != null) && (gpsInterval > 0);
	}
}
